<?php

namespace app\admin\controller;

use app\admin\controller\AdminBase;
use app\admin\model\Page;
use app\common\model\PermissionsModel;
use app\common\model\RoleAndPermissionsModel;
use app\common\model\RoleModel;

class Role extends AdminBase
{

    // 首页
    public function index()
    {
        $role = new RoleModel();
        $role_count = $role->get_role_data_count();
        $size = config('ams_config.role_page_size');

        $page = new Page();
        $page_res = $page->getPageRes($role_count, $size, url('index'));

        $role_list = $role->role_list_page($page_res['offset'], $page_res['size']);

        return view('index', [
            // 分类列表
            'role_list' => $role_list,
            // 若分类无数据，则输出设置的样式
            'empty' => '<tr><td colspan="7" style="text-align: center;">没有数据</td></tr>',
            // 分页的处理结果
            'page_res' => $page_res,
        ]);
    }

    // 添加一个角色
    public function role_add()
    {

        if (request()->post()) {

            $name = input('post.name/s', null);
            $status = input('post.status/d', null);
            $note = input('post.note/s', null);

            if (is_null($name) || is_null($status) || is_null($note)) {
                return json(['code' => 1, 'msg' => '数据不完整']);
            }
            $aid = session('admin_login')->id;
            $role = new RoleModel();
            $role_res =  $role->role_add($aid, $name, $status, $note);
            if (!$role_res) {
                return json(['code' => 2, 'msg' => '角色名称重复']);
            }
            return json(['code' => 0, 'msg' => '添加成功']);
        }

        return view('role_add', [
            // 权限的下拉列表
            'role_status' => config('ams_config.role_status'),
        ]);
    }

    // 角色编辑
    public function role_edit()
    {
        if (request()->post()) {
            $id = input('post.id/d', null);
            if (is_null($id)) {
                return json(['code' => 1, 'msg' => 'ID 不存在']);
            }
            $name = input('post.name/s', null);
            $status = input('post.status/d', null);
            $note = input('post.note/s', null);
            $time = time();

            if (mb_strlen($name) < 2) {
                return json(['code' => 2, 'msg' => '角色名称不能少于2位']);
            }

            if (is_null($status)) {
                return json(['code' => 3, 'msg' => '角色状态未获取']);
            }

            $role = new RoleModel();
            $role_res = $role->set_role_info($id, $name, $status, $note, $time);

            if (!$role_res) {
                return json(['code' => 4, 'msg' => '更新失败']);
            }

            return json(['code' => 0, 'msg' => '更新成功']);
        }

        $rid = input('get.rid/d', null);
        if (is_null($rid)) {
            return redirect_index('角色ID不正确', url('Role/index'));
        }

        $role = new RoleModel();
        $role_info = $role->get_role_info_by_id($rid);
        if (is_null($role_info)) {
            return redirect_index('角色信息不存在', url('Role/index'));
        }

        return view('role_edit', [
            // 角色信息
            'role_info' => $role_info,
            // 权限的下拉列表
            'role_status' => config('ams_config.role_status'),
        ]);
    }

    // 删除一个角色
    public function role_del()
    {
        if (request()->post()) {
            $rid = input('post.rid/d', null);
            if (is_null($rid)) {
                return json(['code' => 1, 'msg' => 'ID 不存在']);
            }

            $role = new RoleModel();
            $role_res = $role->role_del($rid);
            if (!$role_res) {
                return json(['code' => 2, 'msg' => '删除失败,有用户正在使用']);
            }

            return json(['code' => 0, 'msg' => '删除成功']);
        }
        return json(['code' => 99, 'msg' => '请求格式错误']);
    }

    // 授权页面
    public function role_permissions(){

        $rid = input('get.rid/d',null);
        if(is_null($rid)){
            return redirect_index('未传入用户ID', url('Role/index'));
        }

        $role = new RoleModel();
        $role_info = $role->get_role_info_by_id($rid);
        if (!$role_info) {
            return redirect_index('该角色不存在', url('User/index'));
        }

        $permissions = new PermissionsModel();
        $permissions_info = $permissions->get_all_permissions();

        $role_permissions = new RoleAndPermissionsModel();
        $role_permissions_info = $role_permissions->get_role_permissions_info($rid);

        return view('role_permissions',[
            // 角色信息
            'role_info' => $role_info,
            // 所有权限信息
            'permissions_info' => $permissions_info,
            // 角色用户关联信息
            'role_permissions_info' => $role_permissions_info,
        ]);
    }

    // 授权
    public function add_role_permissions()
    {
        if (request()->post()) {
            $rid = input('post.rid/d', null);
            $pid = input('post.pid/d', null);

            if (is_null($pid) || is_null($rid)) {
                return json(['code' => 1, 'msg' => '授权失败，请刷新重试']);
            }

            $aid = session('admin_login')->id;
            $role_permissions = new RoleAndPermissionsModel();
            $res = $role_permissions->add_role_permissions($aid, $rid, $pid);
            if (!$res) {
                return json(['code' => 2, 'msg' => '该权限已存在']);
            }

            return json(['code' => 0, 'msg' => '授权成功']);
        }
        return json(['code' => 99, 'msg' => '请求模式错误']);
    }

    // 取消授权
    public function del_role_permissions()
    {
        if (request()->post()) {
            $role_permissions_id = input('post.id/d', null);
            if (is_null($role_permissions_id)) {
                return json(['code' => 1, 'msg' => '取消失败，请刷新重试']);
            }

            $role_permissions = new RoleAndPermissionsModel();
            $res = $role_permissions->del_role_permissions($role_permissions_id);
            if (!$res) {
                return json(['code' => 2, 'msg' => '取消失败，请刷新重试']);
            }

            return json(['code' => 0, 'msg' => '取消授权成功']);
        }
        return json(['code' => 99, 'msg' => '请求模式错误']);
    }
}
